﻿using System;
using SrBBSPro.Util;
using System.Collections.Generic;
using SrBBSPro.Entities;
using SrBBSPro.BaseClasses;
//using AdvanceSoftware.ExcelCreator.Xlsx;
using System.IO;

namespace SrBBSPro.Services
{
    /// <summary>
    /// 技能Service
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SkillService<T> : BaseUserService<T> where T : BaseUserEntity
    {

        public List<T> GetList(string depart, out int count, int page)
        {
            var li = GetListWithUser();
            if (!string.IsNullOrEmpty(depart))
            {
                li= li.FindAll(t => t.User.Depart ==depart);
            }
            return GetLimitList(li, out count, page);
        }

        public T GetSkillEntity(string userId)
            =>  GetEntityWithUser(userId);      

        /// <summary>
        /// Excel文件生成
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetExcelFile(string path)
        {
            string excel = null;
            //try
            //{
            //    var users = DbBase.Query<User>().ToList();
            //    var jap = DbBase.Query<JapLang>().ToList();
            //    var dbh = DbBase.Query<DBHSkill>().ToList();
            //    var dbo = DbBase.Query<DBOSkill>().ToList();
            //    var hlang = DbBase.Query<HostLang>().ToList();
            //    var olang = DbBase.Query<OpenLang>().ToList();
            //    var os = DbBase.Query<OSSkill>().ToList();
            //    var page = DbBase.Query<PageSkill>().ToList();
            //    var rep = DbBase.Query<ReportSkill>().ToList();
            //    var other = DbBase.Query<OtherLang>().ToList();
            //    var server = DbBase.Query<ServerSkill>().ToList();
            //    var phone = DbBase.Query<PhoneSkill>().ToList();
            //    var script = DbBase.Query<ScriptLang>().ToList();
            //    var cloud = DbBase.Query<CloudLang>().ToList();
            //    var exp = DbBase.Query<Experience>().ToList();
            //    var frm = DbBase.Query<FrameWork>().ToList();
            //    var book = new XlsxCreator();
            //    excel = Path.Combine(path, "Uploads\\Excel\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
            //    book.OpenBook(excel, Path.Combine(path, "Content\\skill.xlsx"));
            //    int row = 4;
            //    for (int i = 0; i < users.Count; i++)
            //    {
            //        var user = users[i];
            //        book.Pos(2, row).Value = user.Id;
            //        book.Pos(3, row).Value = user.StaffName;
            //        book.Pos(4, row).Value = user.DepartName;
            //        book.Pos(5, row).Value = user.Position;
            //        var obj = jap.Find(j => j.Id == user.Id) ?? new JapLang();
            //        int idx = 6;
            //        SetItem(book, jap.Find(j => j.Id == user.Id) ?? new JapLang(), row, ref idx);
            //        SetItem(book, exp.Find(j => j.Id == user.Id) ?? new Experience(), row, ref idx);
            //        SetItem(book, os.Find(j => j.Id == user.Id) ?? new OSSkill(), row, ref idx);
            //        SetItem(book, cloud.Find(j => j.Id == user.Id) ?? new CloudLang(), row, ref idx);
            //        SetItem(book, server.Find(j => j.Id == user.Id) ?? new ServerSkill(), row, ref idx);
            //        SetItem(book, dbo.Find(j => j.Id == user.Id) ?? new DBOSkill(), row, ref idx);
            //        SetItem(book, dbh.Find(j => j.Id == user.Id) ?? new DBHSkill(), row, ref idx);
            //        SetItem(book, phone.Find(j => j.Id == user.Id) ?? new PhoneSkill(), row, ref idx);
            //        SetItem(book, hlang.Find(j => j.Id == user.Id) ?? new HostLang(), row, ref idx);
            //        SetItem(book, page.Find(j => j.Id == user.Id) ?? new PageSkill(), row, ref idx);
            //        SetItem(book, olang.Find(j => j.Id == user.Id) ?? new OpenLang(), row, ref idx);
            //        SetItem(book, frm.Find(j => j.Id == user.Id) ?? new FrameWork(), row, ref idx);
            //        SetItem(book, script.Find(j => j.Id == user.Id) ?? new ScriptLang(), row, ref idx);
            //        SetItem(book, rep.Find(j => j.Id == user.Id) ?? new ReportSkill(), row, ref idx);
            //        SetItem(book, other.Find(j => j.Id == user.Id) ?? new OtherLang(), row, ref idx);
            //        row++;
            //    }
            //    book.CloseBook(true);
            //}
            //catch
            //{
            //    return null;
            //}
            return excel;
        }
      
        /// <summary>
        /// 获取转义字段的值
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        //private void SetItem(XlsxCreator book, object obj, int row, ref int idx)
        //{
        //    foreach (var prop in obj.GetType().GetProperties())
        //    {
        //        if (prop.Name.Equals("Id") || prop.Name.Equals("User"))
        //            continue;
        //        var val = prop.GetValue(obj);
        //        if (val != null && val.GetType() == typeof(System.String) && !prop.Name.Equals("Level"))
        //        {
        //            val = Convert.ToString(val).SkillLevel();
        //        }
        //        book.Pos(idx, row).Value = val;
        //        idx++;
        //    }
        //}

    }
}
